<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    *{
      margin: 0;
      padding: 0;
      list-style: none;
    }
    .flex{
      width: 300px;
      display: flex;
    }
    .flex li{
      flex: 1;
      text-align: center;
    }
    .active{
      background: red;
      color: bisque;
    }
  </style>
</head>
<body>
  <div id="app">
    <lx-tabs @change="changes">
      <lx-tab-item label="开心">今天放假<button>hhhh</button></lx-tab-item>
      <lx-tab-item label="不开心">下周上班</lx-tab-item>
      <lx-tab-item label="难受">下下周上班</lx-tab-item>
    </lx-tabs>
    <!-- <ul class="flex">
      <li :class="{active:currentIndex == i}" v-for="(item,i) in tabs" @click="currentIndex = i">{{item.title}}</li>
    </ul>
    <ul>
      <li v-show="currentIndex == i" v-for="(item,i) in tabs">{{item.content}}</li>
    </ul> -->
  </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
  Vue.component('lx-tabs',{
    data(){
      return{
        tabs:[],
        currentIndex:0
      }
    },
    watch:{
      currentIndex(newval,old){
      this.$children[old].isShow = false;
      this.$children[newval].isShow = true;
      this.$emit('change',newval)
      }
    },
    mounted(){
      this.$children[this.currentIndex].isShow = true;
    },
    template:`
      <div>
        <ul class="flex">
          <li :class="{active:currentIndex == i}" v-for="(item,i) in tabs" @click="currentIndex = i">{{item}}</li>
        </ul>
        <slot></slot>
      </div>
    `
  })
  Vue.component('lx-tab-item',{
    props:['label'],
    data(){
      return {
        isShow:false
      }
    },
    created(){
      // console.log(this.label);
      this.$parent.tabs.push(this.label)
    },
    template:`
      <div v-show='isShow'>
        <slot></slot>  
      </div>
    `
  })
  new Vue({
    el:'#app',
    methods:{
      changes(e){console.log(e);}
    },
    data() {
      return {
        currentIndex:0,
        tabs:[
          {
            title:'开心',
            content:'今天放假'
          },
          {
            title:'不开心',
            content:'下周上班'
          },
          {
            title:'难受',
            content:'下下周上班'
          }
        ]
      }
    },
  })
</script>
</html>